package com.zrrd.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.model.product.*;
import com.zrrd.product.mapper.*;
import com.zrrd.product.service.CategoryService;
import com.zrrd.service.cache.GmallCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private BaseCategory1Mapper category1Mapper;
    @Autowired
    private BaseCategory2Mapper category2Mapper;
    @Autowired
    private BaseCategory3Mapper category3Mapper;
    @Autowired
    private BaseAttrInfoMapper attrInfoMapper;
    @Autowired
    private BaseAttrValueMapper attrValueMapper;
    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;
    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Override
    public List<BaseCategory1> getCategory1() {
        return category1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1_id) {
        return category2Mapper.selectList(new QueryWrapper<BaseCategory2>().
                eq("category1_id", category1_id));
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2_id) {
        return category3Mapper.selectList(new QueryWrapper<BaseCategory3>().
                eq("category2_id", category2_id));
    }

    @Override
    public List<BaseAttrInfo> getBaseAttrInfo(Long category1Id, Long category2Id, Long category3Id) {
        return attrInfoMapper.getBaseAttrInfo(category1Id, category2Id, category3Id);
    }

    @Override
    @Transactional
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //判断是新增还是修改
        if (baseAttrInfo.getId() == null) {
            //给平台属性表添加数据
            attrInfoMapper.insert(baseAttrInfo);
        } else {
            //修改平台属性表数据
            attrInfoMapper.updateById(baseAttrInfo);
            //删除原本关联的属性值列表
            QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id", baseAttrInfo.getId());
            attrValueMapper.delete(wrapper);
        }
        //获取参数中属性值的集合
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        //遍历集合
        for (BaseAttrValue baseAttrValue : attrValueList) {
            //给当前属性值设置关联的属性id
            baseAttrValue.setAttrId(baseAttrInfo.getId());
            //给平台属性对应的属性值添加数据
            attrValueMapper.insert(baseAttrValue);
        }

    }

    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", attrId);
        return attrValueMapper.selectList(wrapper);
    }

    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        return baseSaleAttrMapper.selectList(null);
    }

    @Override
    @GmallCache(prefix = "CategoryView:")
    public BaseCategoryView getBaseCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    @Override
    public List<JSONObject> getBaseCategoryList() {
        //定义最终要返回的List集合
        List<JSONObject> list = new ArrayList<>();
        //查询全部分类的视图
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        for (BaseCategoryView baseCategoryView : baseCategoryViews) {
            System.out.println(baseCategoryView);
        }
        //将视图的集合使用stream流进行拆分（根据1级分类id进行分组） 返回的map集合 key：1级分类id value：当前1级分类id 对应的所有数据
        Map<Long, List<BaseCategoryView>> categoryMap = baseCategoryViews.stream().collect(Collectors.groupingBy(
                BaseCategoryView::getCategory1Id));
        //定义1级分类的编号
        int index = 1;
        //获取1级分类下的所有数据
        for (Map.Entry<Long, List<BaseCategoryView>> entry : categoryMap.entrySet()) {
            //获取1级分类id
            Long category1Id = entry.getKey();
            //获取1级分类下的所有分类集合
            List<BaseCategoryView> category2List = entry.getValue();
            //创建1级分类的JSON对象
            JSONObject category1 = new JSONObject();
            //给1级分类json对象设置属性
            category1.put("index", index);
            category1.put("categoryId",category1Id);
            category1.put("categoryName", category2List.get(0).getCategory1Name());
            index++;
            //将当前1级分类对应的集合拆分（根据2级分类id进行分组）
            Map<Long, List<BaseCategoryView>> category2Map = category2List.stream().collect(Collectors.groupingBy(
                    BaseCategoryView::getCategory2Id));
            //创建二级分类的JOSN对象集合
            List<JSONObject> category2Child = new ArrayList<>();
            //遍历2级分类的map集合
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
                //获取二级分类的id
                Long catgeorg2Id = entry2.getKey();
                //获取二级分类下的所有集合
                List<BaseCategoryView> category3List = entry2.getValue();
                //创建二级分类的JSON对象
                JSONObject category2 = new JSONObject();
                //给二级分类的json对象设置属性
                category2.put("categoryId",catgeorg2Id);
                category2.put("categoryName", category3List.get(0).getCategory2Name());
                //将二级分类的json对象 添加进二级分类的json集合
                category2Child.add(category2);
                //创建3级分类的json集合
                List<JSONObject> category3Child = new ArrayList<>();
                //剩下的category3List其实就是3级分类的数据了 没有必要拆分了
                for (BaseCategoryView category3View : category3List) {
                    //创建3级分类的json对象
                    JSONObject category3 = new JSONObject();
                    //给对象设置属性
                    category3.put("categoryId",category3View.getCategory3Id());
                    category3.put("categoryName",category3View.getCategory3Name());
                    //将3级分类的json对象添加进三级分类的json集合
                    category3Child.add(category3);
                }
                //将三级分类的集合设置给二级分类的对象
                category2.put("categoryChild",category3Child);
            }
            //将二级分类的集合 添加进1级分类对象
            category1.put("categoryChild",category2Child);
            list.add(category1);
        }
        return list;
    }
}
